﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MyMainSite.Models;

namespace MyMainSite.Repositories
{
    public class ProjectRepositoryImplemented : IProjectRepository
    {
        #region Public Methods

        public int GetBlogCountByProjectId(int projectId)
        {
            RichardsSiteEntities context = new RichardsSiteEntities();

            var count = from b in context.Blogs
                        where b.ProjectId == projectId
                        select b.BlogEntries.Count();

            return count.SingleOrDefault();
        }

        public int GetBlogCountByProjectIdAndDate(int projectId, DateTime date)
        {
            RichardsSiteEntities context = new RichardsSiteEntities();

            var count = (from b in context.BlogEntries
                         where b.Blog.ProjectId == projectId &&
                               b.DateCreated.Year == date.Year &&
                               b.DateCreated.Month == date.Month
                         select b).Count();

            return count;
        }

		public List<Project> GetProjects()
		{
			RichardsSiteEntities context = new RichardsSiteEntities();

			var projects = (from p in context.Projects
							orderby p.Name ascending
							select p).ToList();

			return projects;
		}

		public Project GetProjectById(int projectId)
		{
			RichardsSiteEntities context = new RichardsSiteEntities();

			var project = (from p in context.Projects
						   orderby p.Name ascending
						   where p.ProjectId == projectId
						   select p).SingleOrDefault();

			return project;
		}

		public Project GetProjectByName(string name)
		{
			if (String.IsNullOrWhiteSpace(name))
				throw new ArgumentException("name");

			name = name.Trim().ToLower();

			RichardsSiteEntities context = new RichardsSiteEntities();

			var project = (from p in context.Projects
						   orderby p.Name ascending
						   where p.Name.Trim().ToLower() == name
						   select p).SingleOrDefault();

			return project;
		}

		public Project GetProjectByFriendlyUrl(string friendlyUrl)
		{
			if (String.IsNullOrWhiteSpace(friendlyUrl))
				throw new ArgumentException("friendlyUrl");

			friendlyUrl = friendlyUrl.Trim().ToLower();

			RichardsSiteEntities context = new RichardsSiteEntities();

			var project = (from p in context.Projects
						   orderby p.Name ascending
						   where p.FriendlyUrl.Trim().ToLower() == friendlyUrl
						   select p).SingleOrDefault();

			return project;
		}

		public bool IsProjectNameUnique(string name)
		{
			if (String.IsNullOrWhiteSpace(name))
				throw new ArgumentException("name");

			return this.GetProjectByName(name) == null;
		}

		public bool IsProjectFriendlyUrlUnique(string friendlyUrl)
		{
			if (String.IsNullOrWhiteSpace(friendlyUrl))
				throw new ArgumentException("friendlyUrl");

			return this.GetProjectByFriendlyUrl(friendlyUrl) == null;
		}

		public int AddProject(string name, string description, string friendlyUrl, string sourceCodeUrl,
			string bugzXUrl, string androidMarketUrl, byte[] imageData = null)
		{
			if (String.IsNullOrWhiteSpace(name))
				throw new ArgumentException("name");
			if (String.IsNullOrWhiteSpace(description))
				throw new ArgumentException("description");
			if (String.IsNullOrWhiteSpace(friendlyUrl))
				throw new ArgumentException("friendlyUrl");

			RichardsSiteEntities context = new RichardsSiteEntities();

			Project p = new Project();
			p.Name = name.Trim();
			p.Description = description.Trim();
			p.FriendlyUrl = friendlyUrl.Trim();
			p.SourceCodeUrl = sourceCodeUrl.Trim();
			p.BugzXUrl = bugzXUrl.Trim();
			p.AndroidMarketUrl = androidMarketUrl.Trim();

			try
			{
				context.Projects.AddObject(p);
				context.SaveChanges();
			}
			catch (Exception ex)
			{
				// TODO: implement logging into DotNetX
				return 0;
			}

			if (imageData != null)
			{
				IProjectScreenShotRepository screenShotRepository = new ProjectScreenShotRepositoryImplemented();
				if (!screenShotRepository.AddScreenShot(p.ProjectId, imageData))
					return 0;
			}

			return p.ProjectId;
		}

        #endregion
    }
}